package com.example.petstore.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.petstore.common.CommonResponse;
import com.example.petstore.domain.User;
import com.example.petstore.domain.UserInfo;
import com.example.petstore.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userInfoService")
public class UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    public CommonResponse getUserInfoByUsername(String username) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        if(userInfo == null){
            return CommonResponse.creatForError("用户不存在");
        }else{
            return CommonResponse.creatForSuccess(userInfo);
        }
    }
    public List<UserInfo> getUserInfoList() {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
        return userInfoList;
    }

    public CommonResponse saveUserInfo(String username, UserInfo userInfo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        userInfoMapper.delete(queryWrapper);
        userInfo.setUsername(username);
        int result = userInfoMapper.insert(userInfo);
//        QueryWrapper queryWrapper1 = new QueryWrapper();
//        queryWrapper1.eq("userid", userid);


        if(result == 1){
//            return CommonResponse.creatForSuccess(userInfoMapper.selectOne(queryWrapper1));
            return CommonResponse.creatForSuccess("修改成功");
        }else{
            return CommonResponse.creatForError("修改失败");
        }
    }
}
